package com.ruoyi.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi. business.domain.BizCoupon;
import com.ruoyi. business.service.IBizCouponService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 券码信息Controller
 * 
 * @author ruoyi
 * @date 2024-05-19
 */
@RestController
@RequestMapping("/business/coupon")
public class BizCouponController extends BaseController
{
    @Autowired
    private IBizCouponService bizCouponService;

    /**
     * 查询券码信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizCoupon bizCoupon)
    {
        startPage();
        List<BizCoupon> list = bizCouponService.selectBizCouponList(bizCoupon);
        return getDataTable(list);
    }

    /**
     * 导出券码信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:coupon:export')")
    @Log(title = "券码信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizCoupon bizCoupon)
    {
        List<BizCoupon> list = bizCouponService.selectBizCouponList(bizCoupon);
        ExcelUtil<BizCoupon> util = new ExcelUtil<BizCoupon>(BizCoupon.class);
        util.exportExcel(response, list, "券码信息数据");
    }

    /**
     * 获取券码信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:coupon:query')")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId)
    {
        return success(bizCouponService.selectBizCouponByCouponId(couponId));
    }

    /**
     * 新增券码信息
     */
    @PreAuthorize("@ss.hasPermi('business:coupon:add')")
    @Log(title = "券码信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizCoupon bizCoupon)
    {
        bizCoupon.setTenantId(getUserId());
        bizCoupon.setTenantName(getUsername());
        bizCoupon.setCreateBy(getUsername());
        return toAjax(bizCouponService.insertBizCoupon(bizCoupon));
    }

    /**
     * 修改券码信息
     */
    @PreAuthorize("@ss.hasPermi('business:coupon:edit')")
    @Log(title = "券码信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizCoupon bizCoupon)
    {
        bizCoupon.setUpdateBy(getUsername());
        return toAjax(bizCouponService.updateBizCoupon(bizCoupon));
    }
    @PutMapping("/changeStatus")
    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    public AjaxResult changeStatus(@RequestBody BizCoupon bizCoupon)
    {
        bizCoupon.setUpdateBy(getUsername());
        return toAjax(bizCouponService.updateBizCoupon(bizCoupon));
    }

    /**
     * 删除券码信息
     */
    @PreAuthorize("@ss.hasPermi('business:coupon:remove')")
    @Log(title = "券码信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds)
    {
        return toAjax(bizCouponService.deleteBizCouponByCouponIds(couponIds));
    }
    @PreAuthorize("@ss.hasPermi('business:coupon:remove')")
    @Log(title = "券码审核信息", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody BizCoupon bizCoupon)
    {
        bizCoupon.setUpdateBy(getUsername());
        bizCoupon.setUpdateTime(DateUtils.getNowDate());
        return toAjax(bizCouponService.updateBizCoupon(bizCoupon));
    }
}
